
const md5 = require('md5');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/usermanage_0215');

mongoose.connection.on('open',async ()=>{
    // 数据库连接成功
    console.log("数据库连接成功");

    // 创建初始化用户信息
    const userModel = require('./models/userModel');

    let user =await userModel.find({username:"admin"});
    // console.log('打印内容',user);
    if(!user.length){
        let res =await userModel.create({
            username:"admin",
            pwd:md5('111111'),
            profile:"/upload/admin.png"
        });
    
        for(let i=0;i<30;i++){
            userModel.create({
                username:"user_"+i,
                pwd:md5('123'),
                profile:"/upload/admin.png"
            })
        }
    }


})

mongoose.connection.on('error',()=>{
    console.log("数据库连接失败");
})

module.exports = mongoose;